<template>
    <!-- 对话框表单 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="baTable.form.operate ? true : false"
        @close="baTable.toggleForm"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <el-radio-group v-model="state.activeName" style="margin: 20px 0" @change="getData">
                <el-radio-button label="1" >箱->货</el-radio-button>
                <el-radio-button label="2">货->箱</el-radio-button>
            </el-radio-group>

            <div class="main-body" v-if="state.activeName == '1'">
                <div class="left">
                    <h4>箱号</h4>
                    <ul>
                        <li v-for="(item) in state.fpgood" :class="{active:state.currentItem.name==item.name}" @click="handleItemClick(item)">{{item.name}}</li>
                    </ul>
                </div>
                <div class="right">
                    <h4>货物</h4>
                    <div>
                        <el-checkbox-group v-model="state.currentItem.checkedList">
                            <el-checkbox v-for="item in state.good" :label="item.GOODSID" @change="(bl:any)=>handleSave(bl,item)">{{ item.GOODS }}</el-checkbox>
                        </el-checkbox-group>
                    </div>
                </div>
            </div>
            <div class="main-body" v-if="state.activeName == '2'">
                <div class="left">
                    <h4>货物</h4>
                    <ul>
                        <li v-for="(item) in state.fpgood" :class="{active:state.currentItem.name==item.name}" @click="handleItemClick(item)">{{item.name}}</li>
                    </ul>
                </div>
                <div class="right">
                    <h4>箱号</h4>
                    <div>
                        <el-checkbox-group v-model="state.currentItem.checkedList">
                            <el-checkbox v-for="item in state.box" :label="item" @change="(bl:any)=>handleSave(bl,item)">{{ item }}</el-checkbox>
                        </el-checkbox-group>
                    </div>
                </div>
            </div>
            <div class="tip">! 点击时自动保存</div>
        </el-scrollbar>
        <!-- <template #footer v-if="baTable.form.operate !== 'view'">
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
                <el-button type="primary" @click="handleSave">保存</el-button>
            </div>
        </template> -->
    </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, inject, onMounted, computed } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import type { ElForm, FormItemRule } from 'element-plus'
import { buildValidatorData } from '/@/utils/validate'
import createAxios from '/@/utils/axios'
import { stat } from 'fs/promises'


const formRef = ref<InstanceType<typeof ElForm>>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    KTXTIME: [buildValidatorData({name: 'date', message: t('Please enter the correct field', { field: t('xPotfp.KTXTIME') })})],
    STARTDATE: [buildValidatorData({name: 'date', message: t('Please enter the correct field', { field: t('xPotfp.STARTDATE') })})],
    ENDDATE: [buildValidatorData({name: 'date', message: t('Please enter the correct field', { field: t('xPotfp.ENDDATE') })})],
    YJENDDATE: [buildValidatorData({name: 'date', message: t('Please enter the correct field', { field: t('xPotfp.YJENDDATE') })})],
    LOCKDATE: [buildValidatorData({name: 'date', message: t('Please enter the correct field', { field: t('xPotfp.LOCKDATE') })})],
    OPTIME: [buildValidatorData({name: 'date', message: t('Please enter the correct field', { field: t('xPotfp.OPTIME') })})],
})

const state = reactive({
    activeName:'1',
    currentItem:{
        name:'',
        checkedList:[]
    },
    box:[],
    fpgood:{},
    good:[]
})

const TKID = computed(()=>{
    return baTable.form.items!.TKID
    // return 'TKSQ20210121160428'
})

onMounted(()=>{
    getData()
})

const getData = ()=>{
    createAxios({
        url:'admin/XPotsq/index_goodxq',
        params:{
            TKID:TKID.value,
            CORP:'碧辟润滑油（测试）',
            TYPE:state.activeName
        }
    }).then((res:any)=>{
        state.box = res.data.box
        state.good = res.data.good
        if(state.activeName == '1'){
            state.fpgood = state.box.map(item=>{
                return {
                    name:item,
                    checkedList:res.data.fpgood[item]||[]
                }
            })
        }else{
            state.fpgood = state.good.map((item:any)=>{
                item.name = item.GOODS
                item.checkedList = res.data.fpgood[item.GOODSID]||[]
                return item
            })
        }
        state.currentItem = state.fpgood[0]||{}
        console.log(state.currentItem)
    })
}

const handleSave = (bl:Boolean,item:any)=>{
    console.log(item,state.currentItem.checkedList)
    createAxios({
        url:'admin/XPotsq/add_good',
        method:'post',
        data:{
            TCNO: state.activeName=='1'?state.currentItem.name:state.currentItem.checkedList,
            GOODSID:state.activeName=='1'?state.currentItem.checkedList:state.currentItem.GOODSID,
            TKID:TKID.value,
        }
    })
}


const handleItemClick = (item:any)=>{
    console.log(item)
    state.currentItem = item
}
</script>

<style scoped lang="scss">
ul{
    border: 1px solid #eee;
    border-bottom: none;
    li{
        list-style: none;
        padding: 8px 10px;
        border-bottom: 1px solid #eee;
        cursor: pointer;
        &.active{
            background: var(--el-color-primary);
            color:#fff;
        }
    }
}
.main-body{
    display: flex;
    padding: 40px 50px;
    width: 800px;
    .right{
        margin-left: 60px;
    }
    h4{
        font-size: 16px;
        margin-bottom: 16px;
    }
    .el-checkbox-group{
        border: 1px solid #eee;
        border-bottom: none;
        >label{
            width: 100%;
            display: block;
            padding: 8px 10px;
            border-bottom: 1px solid #eee;
        }
    }
}
.tip{
    font-size: 12px;
    margin-top:20px;
    margin-left: 50px;
    color: #999;

}
</style>
